<?php
namespace Admin\Controller;
class HousePictureController extends CommonController{
    public function _initialize() {
		parent::_initialize();
        $this->_mod = D('HousePicture');
        $this->_cate_mod = D('Linkmenu');
        $this->menu = getLinkMenuFor(5);
        $this->house_id = intval($_GET['house_id']);
        $this->url = '&house_id='.$this->house_id;
        $this->house = getHouseName($this->house_id,true,'id,title');
    }
    public function _before_index() {
        $p = I('p',1,'intval');
        $this->assign('p',$p);
    }

    protected function _search() {
        $map = array();
        $map['house_id'] = $this->house_id;
        ($time_start = I('get.time_start')) && $map['add_time'][] = array('egt', strtotime($time_start));
        ($time_end = I('get.time_end')) && $map['add_time'][] = array('elt', strtotime($time_end)+(24*60*60-1));
        ($keyword = I('get.keyword')) && $map['title'] = array('like', '%'.$keyword.'%');
        $cate_id = I('get.cate_id');
		if( $_GET['status']==null ){
            $status = -1;
        }else{
            $status = intval($_GET['status']);
        }
        $status>=0 && $map['status'] = array('eq',$status);
        $selected_ids = '';
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id'=>$cate_id))->getField('spid');
            $selected_ids = $spid ? $spid . $cate_id : $cate_id;
        }

        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'cate_id' => $cate_id,
            'selected_ids' => $selected_ids,
            'status'  => $status,
            'keyword' => $keyword
        ));
        return $map;
    }

    public function add() {
        if(IS_POST){
            $data = $_POST;
            if(!$data['cate_id']){
                $this->error('请选择栏目');
            }
            if(isset($_POST['pic']) && !empty($_POST['pic'])) {
                $insert = array();
                $images = $_POST['pic'];
                foreach ($images['pic'] as $key => $v) {
                    $this->imgWatermark(".$v");
                    $insert[] = array('img' => $v,'title' => $images['alt'][$key],'house_id'=>$data['house_id'],'cate_id'=>$data['cate_id'],'type_id'=>$data['type_id'],'add_time'=>time());
                }
                if(!$this->_mod->addAll($insert)){
                    $this->error('图片上传失败');
                }else{
                    D('count')->where(array('house_id'=>$data['house_id']))->setInc('photo',count($insert));
                    $this->success('图片上传成功');
                }
            }else{
                $this->error('请选择图片');
            }
        }else{
            $this->display();
        }

    }
    public function delete(){
        $mod = $this->_mod;
        $pk = $mod->getPk();
        $ids = trim(I($pk), ',');
        if ($ids) {
            $lists = $mod->where(array('id'=>array('in',$ids)))->field('img,house_id')->select();
            if (false !== $mod->delete($ids)) {
                if($lists){
                    $modelarr = array();
                    foreach($lists as $v){
                        @unlink('.'.$v['img']);
                        $modelarr['house'][$v['house_id']] +=1 ;
                    }
                    foreach($modelarr as $k=>$vs){
                        foreach($vs as $key=>$n){
                            D('count')->where(array('house_id'=>$key,'model'=>$k))->setDec('photo',$n);
                        }
                    }
                }
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'));
                $this->success(L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                $this->error(L('operation_failure'));
            }
        } else {
            IS_AJAX && $this->ajaxReturn(0, L('illegal_parameters'));
            $this->error(L('illegal_parameters'));
        }
    }

    public function _before_edit(){
        $id = I('get.id');
        $info = $this->_mod->field('id,cate_id,house_id')->where(array('id'=>$id))->find();
        $spid = $this->_cate_mod->where(array('id'=>$info['cate_id']))->getField('spid');
        if( $spid==0 ){
            $spid = $info['cate_id'];
        }else{
            $spid .= $info['cate_id'];
        }
        $this->assign('selected_ids',$spid);
    }

    protected function _before_update($data) {
        return $this->_updatePic($data);
    }


}